package com.wk.springboot.link;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @Classname NodeUtils
 * @Description node的工具类
 * @Date 2020/8/28 0:05
 * @Author by weikai
 */
public class NodeUtils {

  private NodeUtils(){}

  private static List<Integer> sortNodeAnd2List(List<OneWayNode<Integer>> list){

    List<Integer> resultList = new ArrayList();
    OneWayNode<Integer> node = null;
    list.forEach(val->{
      OneWayNode<Integer> tempNode = val;
      resultList.add(tempNode.el);
      while (tempNode.next != null){
        resultList.add(tempNode.next.el);
        tempNode = tempNode.next;
      }
    });

    Collections.sort(resultList);

    return resultList;
  }

  private static OneWayNode<Integer> list2Node(List<Integer> list){

    OneWayNode<Integer> resultNode = null;
    OneWayNode<Integer> tmpNode = null;
    for(int i = 0;i < list.size(); i++){
      OneWayNode<Integer> newNode = new OneWayNode<>(list.get(i),null);
      if(i == 0){
        resultNode = newNode;
      }else if(i == 1){
        resultNode.next = newNode;
      }else {
        tmpNode.next = newNode;
      }
      tmpNode = newNode;
    }
    return resultNode;
  }

  public static OneWayNode<Integer> sortNodeList(List<OneWayNode<Integer>> nodeList){

    List<Integer> resultList = sortNodeAnd2List(nodeList);
    OneWayNode<Integer> resultNode = list2Node(resultList);
    return resultNode;
  }
}
